package com.liuke.study.thread;

/**
 * TODO
 *
 * @author wangjwy
 * @date 2019/10/18 10:02
 * @since 1.0.0
 */
public class Test2 {

	public static void main(String[] args) throws InterruptedException {
		Printer2 a = new Printer2("A");
		Printer2 b = new Printer2("B");
		Printer2 c = new Printer2("C");
		Thread thread1 = new Thread(new Worker2(a));
			Thread thread2 = new Thread(new Worker2(b));
			Thread thread3 = new Thread(new Worker2(c));
		while (true) {
			thread1.start();
			thread1.join();
			thread2.start();
			thread2.join();
			thread3.start();
			thread3.join();
		}
	}


}

class Worker2 implements Runnable {

	private Printer2 printer;

	public Worker2(Printer2 printer) {
		this.printer = printer;
	}

	@Override
	public void run() {

		synchronized (printer) {
			printer.work();
			Thread.yield();
		}
	}
}

class Printer2 {
	private String out;

	public Printer2(String out) {
		this.out = out;
	}

	public void work() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.print(out);
	}

}